package com.prs.crm.dao.oa.impl;

import java.sql.SQLException;
import java.util.Collection;
import java.util.Date;

import org.courser.Util;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.ProjectionList;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.prs.crm.context.Constrains;
import com.prs.crm.dao.impl.BaseDaoImpl;
import com.prs.crm.dao.oa.ScheduleDao;
import com.prs.crm.domain.hr.Person;
import com.prs.crm.domain.oa.Schedule;

public class ScheduleDaoImpl extends BaseDaoImpl<Schedule> implements ScheduleDao{

	@Override
	public Collection<Schedule> getExecuteSchedule(Person person) {
		// TODO Auto-generated method stub
		Criteria criteria = this.getSession().createCriteria(Schedule.class);
		criteria.add(Restrictions.eq("exePerson", person));
		criteria.add(Restrictions.or(Restrictions.eq("state", Constrains.TASK_EXECUTE), Restrictions.eq("state", Constrains.TASK_NOTPASS)));
		return criteria.list();
	}

	@Override
	public Collection<Schedule> getAuditSchedule(Person person) {
		// TODO Auto-generated method stub
		Criteria criteria = this.getSession().createCriteria(Schedule.class);
		criteria.add(Restrictions.eq("allotPerson", person));
		criteria.add(Restrictions.eq("state", Constrains.TASK_AUDITING));
		return criteria.list();
	}

	@SuppressWarnings("unchecked")
	@Override
	public Collection<Object[]> getScheduleReportDataSouce(String state,
			Date datestart, Date dateend, Collection<Person> persons) {
		// TODO Auto-generated method stub
		Criteria criteria = this.getSession().createCriteria(Schedule.class);
		if(state != null)
			criteria.add(Restrictions.eq("state", state));
		if(datestart != null)
			criteria.add(Restrictions.ge("beginTime", datestart));
		if(dateend != null)
			criteria.add(Restrictions.le("beginTime", dateend));
		if(persons != null)
			criteria.add(Restrictions.in("exePerson", persons));
		
		ProjectionList projectionList = Projections.projectionList();
		projectionList.add(Projections.groupProperty("exePerson"));
		projectionList.add(Projections.rowCount());
		criteria.setProjection(projectionList);
		return criteria.list();
	}

	@Override
	@SuppressWarnings("unchecked")
	public Collection<Schedule> getOverdueSchedule(final Person person) {
		// TODO Auto-generated method stub
		return (Collection<Schedule>) this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session arg0)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				Criteria criteria = arg0.createCriteria(Schedule.class);
				criteria.add(Restrictions.eq("exePerson", person));
				criteria.add(Restrictions.eq("state", Constrains.TASK_EXECUTE));
				criteria.add(Restrictions.lt("endTime", Util.nowFullTime()));
				criteria.addOrder(Order.asc("endTime"));
				return criteria.list();
			}
			
		});
	}

	@Override
	@SuppressWarnings("unchecked")
	public Collection<Schedule> getExecuteScheduleByLevel(final Person person,
			final String level) {
		// TODO Auto-generated method stub
		return (Collection<Schedule>)this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session arg0)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				Criteria criteria = arg0.createCriteria(Schedule.class);
				criteria.add(Restrictions.eq("exePerson", person));
				criteria.add(Restrictions.or(Restrictions.eq("state", Constrains.TASK_EXECUTE), Restrictions.eq("state", Constrains.TASK_NOTPASS)));
				criteria.add(Restrictions.eq("level", level));
				return criteria.list();
			}
			
		});
	}

}
